package com.xxl.job.executor.sample.frameless;

import com.xxl.job.executor.sample.frameless.config.CarteXxlJobConfig;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.extension.ExtensionPoint;
import org.pentaho.di.core.extension.ExtensionPointInterface;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @author xuxueli 2018-10-31 19:05:43
 */
@ExtensionPoint( id = "XxlJobExecutorExtension", description = "XxlJobExecutorExtension", extensionPointId = "CarteStartup" )
public class CarteExecutor implements ExtensionPointInterface {
    private static Logger logger = LoggerFactory.getLogger(CarteExecutor.class);

    public void callExtensionPoint(LogChannelInterface log, Object object ) throws KettleException {
        Thread startThread =  new Thread(() -> {
            try {
                // start
                CarteXxlJobConfig.getInstance().initXxlJobExecutor();

                // Blocks until interrupted
                while (true) {
                    try {
                        TimeUnit.HOURS.sleep(1);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            } finally {
                // destory
                CarteXxlJobConfig.getInstance().destoryXxlJobExecutor();
            }
        });
        startThread.setName("Carte Executor Start Thread");
        startThread.start();
    }

}
